﻿using System;
using System.Security;
using System.Text;
using Xunit;

namespace LeRenard.Core.Cryptography.Tests
{
    public class Base64CryptographerTests
    {
        [Fact]
        public void Base64Cryptographer_Implements_ICryptographer()
        {
            // Arrange
            var cryptographer = new Base64Cryptographer(Encoding.Unicode);
            var expected = typeof(ICryptographer<SecureString, string>);

            // Act
            var actual = cryptographer.GetType().GetInterface(expected.Name, true);

            // Assert
            Assert.Equal<Type>(expected, actual);
        }

        [Fact]
        public void Base64Cryptographer_Implements_IDisposable()
        {
            // Arrange
            var cryptographer = new Base64Cryptographer(Encoding.Unicode);
            var expected = typeof(IDisposable);

            // Act
            var actual = cryptographer.GetType().GetInterface(expected.Name, true);

            // Assert
            Assert.Equal<Type>(expected, actual);
        }

        [Fact]
        public void Base64Cryptographer_Uses_Correct_Encoding()
        {
            // Arrange
            var cryptographer = new Base64Cryptographer(Encoding.UTF7);
            var expected = Encoding.UTF7;

            // Act
            var actual = cryptographer.Encoding;

            // Assert
            Assert.Equal<Encoding>(expected, actual);
        }

        [Fact]
        public void Base64Cryptographer_Roundtrip_Gives_Same_Results()
        {
            // Arrange            
            var input = "\"The quick brown fox jumps over the lazy dog\" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is used to show fonts and to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.";
            var expected = input;
            var cryptographer = new Base64Cryptographer(Encoding.Unicode);

            // Act            
            var actualEncrypted = cryptographer.Encode(input.ToSecureString());
            var actualDecrypted = cryptographer.Decode(actualEncrypted).ToUnsecureString();

            // Assert            
            Assert.Equal(expected, actualDecrypted);
        }

        [Fact]
        public void Base64Cryptographer_Can_Encrypt()
        {
            // Arrange            
            var input = "\"The quick brown fox jumps over the lazy dog\" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is used to show fonts and to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.";
            var expected = "IgBUAGgAZQAgAHEAdQBpAGMAawAgAGIAcgBvAHcAbgAgAGYAbwB4ACAAagB1AG0AcABzACAAbwB2AGUAcgAgAHQAaABlACAAbABhAHoAeQAgAGQAbwBnACIAIABpAHMAIABhAG4AIABFAG4AZwBsAGkAcwBoAC0AbABhAG4AZwB1AGEAZwBlACAAcABhAG4AZwByAGEAbQAUIGEAIABwAGgAcgBhAHMAZQAgAHQAaABhAHQAIABjAG8AbgB0AGEAaQBuAHMAIABhAGwAbAAgAG8AZgAgAHQAaABlACAAbABlAHQAdABlAHIAcwAgAG8AZgAgAHQAaABlACAAYQBsAHAAaABhAGIAZQB0AC4AIABJAHQAIABpAHMAIAB1AHMAZQBkACAAdABvACAAcwBoAG8AdwAgAGYAbwBuAHQAcwAgAGEAbgBkACAAdABvACAAdABlAHMAdAAgAHQAeQBwAGUAdwByAGkAdABlAHIAcwAgAGEAbgBkACAAYwBvAG0AcAB1AHQAZQByACAAawBlAHkAYgBvAGEAcgBkAHMALAAgAGEAbgBkACAAaQBuACAAbwB0AGgAZQByACAAYQBwAHAAbABpAGMAYQB0AGkAbwBuAHMAIABpAG4AdgBvAGwAdgBpAG4AZwAgAGEAbABsACAAbwBmACAAdABoAGUAIABsAGUAdAB0AGUAcgBzACAAaQBuACAAdABoAGUAIABFAG4AZwBsAGkAcwBoACAAYQBsAHAAaABhAGIAZQB0AC4AIABPAHcAaQBuAGcAIAB0AG8AIABpAHQAcwAgAGIAcgBlAHYAaQB0AHkAIABhAG4AZAAgAGMAbwBoAGUAcgBlAG4AYwBlACwAIABpAHQAIABoAGEAcwAgAGIAZQBjAG8AbQBlACAAdwBpAGQAZQBsAHkAIABrAG4AbwB3AG4ALgA=";
            var cryptographer = new Base64Cryptographer(Encoding.Unicode);

            // Act            
            var actualEncrypted = cryptographer.Encode(input.ToSecureString());            

            // Assert            
            Assert.Equal(expected, actualEncrypted);
        }

        [Fact]
        public void Base64Cryptographer_Can_Decrypt()
        {
            // Arrange            
            var expected = "\"The quick brown fox jumps over the lazy dog\" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is used to show fonts and to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.";
            var input = "IgBUAGgAZQAgAHEAdQBpAGMAawAgAGIAcgBvAHcAbgAgAGYAbwB4ACAAagB1AG0AcABzACAAbwB2AGUAcgAgAHQAaABlACAAbABhAHoAeQAgAGQAbwBnACIAIABpAHMAIABhAG4AIABFAG4AZwBsAGkAcwBoAC0AbABhAG4AZwB1AGEAZwBlACAAcABhAG4AZwByAGEAbQAUIGEAIABwAGgAcgBhAHMAZQAgAHQAaABhAHQAIABjAG8AbgB0AGEAaQBuAHMAIABhAGwAbAAgAG8AZgAgAHQAaABlACAAbABlAHQAdABlAHIAcwAgAG8AZgAgAHQAaABlACAAYQBsAHAAaABhAGIAZQB0AC4AIABJAHQAIABpAHMAIAB1AHMAZQBkACAAdABvACAAcwBoAG8AdwAgAGYAbwBuAHQAcwAgAGEAbgBkACAAdABvACAAdABlAHMAdAAgAHQAeQBwAGUAdwByAGkAdABlAHIAcwAgAGEAbgBkACAAYwBvAG0AcAB1AHQAZQByACAAawBlAHkAYgBvAGEAcgBkAHMALAAgAGEAbgBkACAAaQBuACAAbwB0AGgAZQByACAAYQBwAHAAbABpAGMAYQB0AGkAbwBuAHMAIABpAG4AdgBvAGwAdgBpAG4AZwAgAGEAbABsACAAbwBmACAAdABoAGUAIABsAGUAdAB0AGUAcgBzACAAaQBuACAAdABoAGUAIABFAG4AZwBsAGkAcwBoACAAYQBsAHAAaABhAGIAZQB0AC4AIABPAHcAaQBuAGcAIAB0AG8AIABpAHQAcwAgAGIAcgBlAHYAaQB0AHkAIABhAG4AZAAgAGMAbwBoAGUAcgBlAG4AYwBlACwAIABpAHQAIABoAGEAcwAgAGIAZQBjAG8AbQBlACAAdwBpAGQAZQBsAHkAIABrAG4AbwB3AG4ALgA=";
            var cryptographer = new Base64Cryptographer(Encoding.Unicode);

            // Act            
            var actualDecrypted = cryptographer.Decode(input).ToUnsecureString();

            // Assert            
            Assert.Equal(expected, actualDecrypted);
        }
    }
}
